package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.ReservationDto;
import com.zzyl.entity.Bed;
import com.zzyl.entity.Reservation;
import com.zzyl.enums.BasicEnum;
import com.zzyl.enums.ReservationStatus;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.ReservationMapper;
import com.zzyl.service.ReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.NursingProjectVo;
import com.zzyl.vo.ReservationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class ReservationServiceImpl implements ReservationService {
    @Autowired
    ReservationMapper reservationMapper;
    @Override
    public Integer getCancelledCount() {
        /**通过ThreadLocal线程获取到当前用户的手机号，然后再根据手机号去member表中查出当前这个人
         * 的id，再根据id去reservation表中查询出当前用户的预约记录，然后统计出取消的预约记录的条数
        */
        Reservation reservation = new Reservation();
        Long mgtUserId = UserThreadLocal.getMgtUserId();//获取当前用户id
        //根据id去查出当前用户的手机号
        String number=reservationMapper.selectNumber(mgtUserId);
        //根据手机号去reservation表中查询出当前用户的预约记录，然后统计出取消的预约记录的条数
        reservation.setMobile(number);
        //根据手机号去reservation表中查询状态为2的记录，即取消的记录返回为Integer类型
        Integer count = reservationMapper.getCancelledCount(reservation);
        return count;
    }

    @Override
    public void add(ReservationDto dto) {
        Long userId = UserThreadLocal.getUserId();
        Reservation reservation = new Reservation();
        BeanUtils.copyProperties(dto, reservation);
        reservation.setStatus(0);
        reservation.setCreateBy(userId);
        reservationMapper.insert(reservation);
    }

    @Override
    public PageResponse<ReservationVo> findByPage(int pageNum, int pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Long userId = UserThreadLocal.getUserId();
        Page<Reservation> byPage = (Page<Reservation>)reservationMapper.findByPage(status, userId);
        return PageResponse.of(byPage, ReservationVo.class);
    }

    @Override
    public void cancelReservation(Long id) {
        //先判断是否有这个预约记录
        Reservation reservation = reservationMapper.findById(id);
        if (reservation == null) {
            throw new BaseException(BasicEnum.RESERVATION_Id_IS_NULL);
        }
        //关于取消订单，只有是待报道的才能取消其余三种都不能取消
        if (reservation.getStatus() != 0) {
            throw new BaseException(BasicEnum.RESERVATION_Status_EXCEED);
        }
        reservationMapper.updateCancel(reservation);
    }

    @Override
    public List<Map<String, Object>> countByTime(Long time) {
        LocalDateTime localDateTime = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = LocalDateTimeUtil.beginOfDay(localDateTime);
        LocalDateTime endTime = LocalDateTimeUtil.endOfDay(localDateTime);
        return reservationMapper.countByTime(startTime, endTime);
    }
}
